package thitracnghiem;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Odotube
 */
public class GiaoVien{
    int id;
    String username;
    String password;
    String hoTenGV;
    String khoa;
    String phanQuyenMon;
    String phanQuyenLop;
    static Connection conn;
    static Statement stm;
    
    public GiaoVien(int id, String username, String password, String hoTenGV, String khoa, String phanQuyenMon, String phanQuyenLop){
        this.id=id;
        this.username=username;
        this.password=password;
        this.hoTenGV=hoTenGV;
        this.khoa=khoa;
        this.phanQuyenMon=phanQuyenMon;
        this.phanQuyenLop=phanQuyenLop;
    }
    
    public static void Open() throws SQLException{
        conn=DriverManager.getConnection("jdbc:odbc:ttn");
        stm=conn.createStatement();
    }
    
    public static int phanQuyen(String username, String phanQuyenMon, String phanQuyenLop) throws SQLException{
        String sql="update GiaoVien set phanquyenmon='"+phanQuyenMon+"',phanquyenlop='"+phanQuyenLop+"' where"
                + " username='"+username+"'";
        return stm.executeUpdate(sql);
    }
    public int insGiaoVien() throws SQLException{
        String sql="insert into GiaoVien values('"+username+"','"+password+"','"+hoTenGV+"','"+khoa
                +"','"+phanQuyenMon+"','"+phanQuyenLop+"')";
        return stm.executeUpdate(sql);
    }
    public static void Close() throws SQLException{
        stm.close();
        conn.close();
    }
    
    public static int changePassword(int id,String password) throws SQLException{
        String sql="update GiaoVien set pass_word='"+password+"' where id="+id;
        return stm.executeUpdate(sql);
    }
    
    public static GiaoVien loginSuccessful(String username, String password) throws SQLException{
       // GiaoVien.Open();
        String sql = "select * from GiaoVien where username='"+username
                +"' and password='"+password+"'";
        ResultSet rs = stm.executeQuery(sql);
        if(rs.next()) {
            //GiaoVien.Close();
            return new GiaoVien(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7));
        }
        else {
            //GiaoVien.Close();
            return null;
        }
    }
    public void setID(int id){
        this.id=id;
    }
    
    public int getID(){
        return this.id;
    }
    
    public void setUsername(String username){
        this.username=username;
    }
    
    public String getUsername(){
        return this.username;
    }
    
    public void setPassword(String password){
        this.password=password;
    }
    
    public String getPassword(){
        return this.password;
    }
    
    public void setHoTenGV(String hoTenGV){
        this.hoTenGV=hoTenGV;
    }
    
    public String getHoTenGV(){
        return this.hoTenGV;
    }
    
    public void setKhoa(String khoa){
        this.khoa=khoa;
    }
    
    public String getKhoa(){
        return this.khoa;
    }
    
    public void setPhanQuyenMon(String phanQuyenMon){
        this.phanQuyenMon=phanQuyenMon;
    }
    
    public String getPhanQuyenMon(){
        return this.phanQuyenMon;
    }
    
    public void setPhanQuyenLop(String phanQuyenLop){
        this.phanQuyenMon=phanQuyenMon;
    }
    
    public String getPhanQuyenLop(){
        return this.phanQuyenLop;
    }
    
    public static void main(String[] args){
        try {
            GiaoVien.Open();
            GiaoVien.phanQuyen("gv", "1", "D09THA1");
            GiaoVien.Close();
        } catch (SQLException ex) {
            Logger.getLogger(GiaoVien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
